<script setup lang="ts">
import { getHomeGoodsGuessAPI } from '@/services/home'
import type { PageParams } from '@/types/global'
import type { GuessItem } from '@/types/home'
import { ref, onMounted } from 'vue'

// 分页参数
const query: PageParams = {
  page: 1, // 页码
  pageSize: 10, // 条数
}

// 数据是否加载结束的标志
const isFinish = ref(false)

const guessList = ref<GuessItem[]>([])
// 获取首页猜你喜欢数据
const getHomeGoodsGuessData = async () => {
  // 如果加载结束了
  if (isFinish.value) {
    // 给用户反馈并提前结束代码的执行
    return uni.showToast({
      title: '别拉了,劳资是有底线的!',
      icon: 'none',
    })
  }
  // 发请求
  const res = await getHomeGoodsGuessAPI(query)
  // 如果当前页码小于总页码数
  if (query.page! < res.pages) {
    // 数组合并操作
    // 旧新数组合并
    // (1) 方式1 - 调用原生的concat()
    // guessList.value = guessList.value.concat(res.items)
    // (2) 方式2 - push()配置展开运算符
    guessList.value.push(...res.items)
    // (3) 方式3 - 纯展开
    // guessList.value = [
    //   ...guessList.value,
    //   ...res.items
    // ]
    // 页码自增
    query.page!++
  } else {
    // 结束加载更多
    isFinish.value = true
  }
}
// 对象暴漏获取更多数据的方法
defineExpose({
  getMore: getHomeGoodsGuessData,
})
onMounted(() => {
  getHomeGoodsGuessData()
})
</script>

<template>
  <!-- 猜你喜欢 -->
  <view class="caption">
    <text class="text">猜你喜欢</text>
  </view>
  <view class="guess">
    <navigator class="guess-item" v-for="item in guessList" :key="item.id" :url="`/pages/goods/goods?id=${item.id}`">
      <image class="image" mode="aspectFill" :src="item.picture"></image>
      <view class="name">
        {{ item.name }}
      </view>
      <view class="price">
        <text class="small">¥</text>
        <text>{{ item.price }}.00</text>
      </view>
    </navigator>
  </view>
  <view class="loading-text">
    {{ isFinish ? '数据加载结束' : '正在加载...' }}
  </view>
</template>

<style lang="scss">
:host {
  display: block;
}

/* 分类标题 */
.caption {
  display: flex;
  justify-content: center;
  line-height: 1;
  padding: 36rpx 0 40rpx;
  font-size: 32rpx;
  color: #262626;

  .text {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 0 28rpx 0 30rpx;

    &::before,
    &::after {
      content: '';
      width: 20rpx;
      height: 20rpx;
      background-image: url(@/static/images/bubble.png);
      background-size: contain;
      margin: 0 10rpx;
    }
  }
}

/* 猜你喜欢 */
.guess {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  padding: 0 20rpx;

  .guess-item {
    width: 345rpx;
    padding: 24rpx 20rpx 20rpx;
    margin-bottom: 20rpx;
    border-radius: 10rpx;
    overflow: hidden;
    background-color: #fff;
  }

  .image {
    width: 304rpx;
    height: 304rpx;
  }

  .name {
    height: 75rpx;
    margin: 10rpx 0;
    font-size: 26rpx;
    color: #262626;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }

  .price {
    line-height: 1;
    padding-top: 4rpx;
    color: #cf4444;
    font-size: 26rpx;
  }

  .small {
    font-size: 80%;
  }
}

// 加载提示文字
.loading-text {
  text-align: center;
  font-size: 28rpx;
  color: #666;
  padding: 20rpx 0;
}
</style>
